<?php 
/**
 * 统计控制器
 * @Author    紫梦
 * @DateTime  2018-01-26
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use app\common\model\Agency;
use app\common\model\Cars;
use app\common\model\Brand;

class Charts extends Base
{
    public function cars()
    {

        $brand = Brand::column('id');

        $model = Brand::withCount(['cars'])->select($brand);
        foreach ($model as $key => $value) {
            $array[$key]['value'] = $value->cars_count;
            
            $array[$key]['name'] = $value->name;
        }
        rsort($array);
        $name = array_column($array,'name');
        $count = Cars::count();
        $this->assign('brand',json_encode($array)); 
        $this->assign('name',json_encode($name));   
        $this->assign('count',$count);  
        return $this->fetch();
    }
    
    public static function relevance()
    {
        $agency = Agency::where('id','>',1)->column('id');
        $type = 0;
        $array = [];
        for ($i=0; $i < 7; $i++) { 
            $model = Agency::withCount(['cars'=>function($query){
                                            global $type;$type++;
                                            $query->where('relevance',$type);
                                        }])->select($agency);
            foreach ($model as $key => $value) {
                $array[0][$key]['value'][$i]= $value->cars_count;
                $array[0][$key]['name'] = $value->name;
                $array[1][0][$i][$key] = $value->cars_count;
                $array[1][1]['name'][$key] = $value->name;
            }
        }
        return $array;
    }
}